package com.qf.cardlikeboot.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cardlikeboot.entity.Crad;
import com.qf.cardlikeboot.request.Request;
import com.qf.cardlikeboot.service.CradService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/crad")
public class CradController {

    @Autowired
    CradService cradService;

    @RequestMapping("showAll")
    public Request<PageInfo<Crad>> cradList(Integer pageNum, Integer pageSize){
        System.out.println(pageNum + "," + pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<Crad> crads = cradService.selectAll();
        PageInfo<Crad> PageInfo = new PageInfo<>(crads);
        return Request.success(PageInfo);
    }

    @GetMapping("browse")
    public Request<Crad> browse(Integer id){
        Crad crad = cradService.selectById(id);
        return Request.success(crad);
    }
    @PostMapping("insert")
    public Request<Crad> insert(Crad crad){
        cradService.insert(crad);
        return Request.success();
    }

    @GetMapping("likes")
    public Request<PageInfo<Crad>> likes(String likeIds,Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Crad> crads = cradService.selectLiked(likeIds);
        PageInfo<Crad> PageInfo = new PageInfo<>(crads);
        return Request.success(PageInfo);
    }

    @GetMapping("selectOneself")
    public Request<Crad> selectOneself(Integer id){
        Crad crad = cradService.selectByPrimaryKey(id);
        return Request.success(crad);
    }

}
